//
//  SettingViewController.m
//  Inforcollection
//
//  Created by macbook on 16/1/14.
//  Copyright © 2016年 oceansoft.liw. All rights reserved.
//

#import "SettingViewController.h"

@interface SettingViewController ()

@end

@implementation SettingViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNaviBarTitle:@"设置"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"KCell"];
    
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"KCell"];
    }

    cell.textLabel.text=@"清理缓存";
    
    
    //获取缓存文件路径
    NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
    
    NSFileManager * manager = [ NSFileManager defaultManager ];
    
    //如果缓存路径为空 则直接返回
    if (![manager fileExistsAtPath :cachPath]) return 0 ;
    
    //枚举出当前文件夹下面的所有文件
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :cachPath] objectEnumerator];
    
    //枚举出来的文件名称
    NSString * fileName;
    
    //文件大小
    long long folderSize = 0 ;
    
    //遍历累积文件大小并统计出来
    while ((fileName = [childFilesEnumerator nextObject]) != nil ){
        //拼接路径名
        NSString * fileAbsolutePath = [cachPath stringByAppendingPathComponent :fileName];
        
        folderSize += [ self fileSizeAtPath :fileAbsolutePath];
        
    }
    
    
    NSString *currentSize = [NSString stringWithFormat:@"%lld",folderSize];
    
    if (folderSize<1024)
    {
        currentSize=[NSString stringWithFormat:@"%lld B",folderSize];
    }
    else if (1024<folderSize && folderSize<1024*1024)
    {
        currentSize=[NSString stringWithFormat:@"%lld KB",folderSize/1024];
    }
    else
    {
        currentSize=[NSString stringWithFormat:@"%lld MB",folderSize/1024/1024];
    }
    
    
    cell.detailTextLabel.text=currentSize;

    return cell;
}

- ( long long ) fileSizeAtPath:( NSString *) filePath{
    
    NSFileManager * manager = [ NSFileManager defaultManager ];
    
    if ([manager fileExistsAtPath :filePath]){
        //计算出文件大小
        return [[manager attributesOfItemAtPath :filePath error : nil ] fileSize ];
    }
    return 0 ;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //清除缓存
    NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
    
    NSArray * files = [[ NSFileManager defaultManager ] subpathsAtPath :cachPath];
    
    NSLog ( @"cachpath = %@" , cachPath);
    
    for ( NSString * p in files) {
        
        NSError * error = nil ;
        
        NSString * path = [cachPath stringByAppendingPathComponent :p];
        
        if ([[ NSFileManager defaultManager ] fileExistsAtPath :path]) {
            
            [[ NSFileManager defaultManager ] removeItemAtPath :path error :&error];
        }
        
    }
    [ self performSelectorOnMainThread : @selector (clearCachSuccess) withObject : nil waitUntilDone : YES ];
}

-(void)clearCachSuccess
{
    [self.mainTableView reloadData];
}
@end
